package utils;

import cn.hutool.core.date.DateUtil;

import jxl.Cell;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.read.biff.BiffException;
import jxl.write.*;

import org.junit.jupiter.api.function.Executable;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/**
 * 把log信息写入手机根目录，文件名为：Mytestlogs.text
 *
 * @author Administrator
 */
public class MyTestResultForExcel {

    /**
     * 创建测试报告文件
     */
    private File creatResultFile() throws IOException {
        File file = new File("MyTestResultForExcel.xls");
        assert file.exists() || file.createNewFile();
        return file;
    }

    /**
     * 初始化测试报告
     *
     * @throws Exception
     */
    public void initTestResultExcel() throws Exception {
        File file = creatResultFile();

        WritableWorkbook wbook = Workbook.createWorkbook(file);
        WritableSheet sheet = wbook.createSheet("自动化测试结果与统计", 0);

        // 合并单元格，第二列到第4列 第一行到第5行
        sheet.mergeCells(1, 0, 5, 0);
        sheet.mergeCells(1, 1, 5, 1);
        sheet.mergeCells(1, 2, 5, 2);
        sheet.mergeCells(1, 3, 5, 3);
        sheet.mergeCells(1, 4, 5, 4);
        // 标记单元格
        WritableFont font = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD, false,
                UnderlineStyle.NO_UNDERLINE, Colour.DARK_BLUE2);
        WritableCellFormat fontFormat = new WritableCellFormat(font);
        Label label0 = new Label(0, 0, "测试设备", fontFormat);
        Label label1 = new Label(0, 1, "测试开始时间", fontFormat);
        Label label2 = new Label(0, 2, "测试结束时间", fontFormat);
        Label label3 = new Label(0, 3, "测试用例总数", fontFormat);
        Label label4 = new Label(0, 4, "测试失败率", fontFormat);
        Label label5 = new Label(0, 5, "测试用例编号", fontFormat);
        Label label6 = new Label(1, 5, "测试项或操作步骤", fontFormat);
        Label label7 = new Label(2, 5, "期望结果", fontFormat);
        Label label8 = new Label(3, 5, "测试结果", fontFormat);
        Label label12 = new Label(4, 5, "时间点", fontFormat);
        Label label11 = new Label(5, 5, "备注", fontFormat);
        Label label9 = new Label(1, 0, "", fontFormat);
        Label label10 = new Label(1, 1, DateUtil.format(DateUtil.date(),"yyyy/MM/dd HH:mm:ss"), fontFormat);

        // 写入数据
        sheet.addCell(label0);
        sheet.addCell(label1);
        sheet.addCell(label2);
        sheet.addCell(label3);
        sheet.addCell(label4);
        sheet.addCell(label5);
        sheet.addCell(label6);
        sheet.addCell(label7);
        sheet.addCell(label8);
        sheet.addCell(label9);
        sheet.addCell(label10);
        sheet.addCell(label11);
        sheet.addCell(label12);
        wbook.write();// 写入
        wbook.close();
    }

    /**
     * 输出报告
     *
     * @param number         测试编号
     * @param operatingSteps 操作步骤
     * @param expected       期望结果
     * @param executable     实际结果
     * @param remark         备注
     */
    public void printTestResult(String number, String operatingSteps, String expected, Executable executable, String remark) {


        try {
            File file = creatResultFile();
            Workbook wbook = Workbook.getWorkbook(file);
            WritableWorkbook book = Workbook.createWorkbook(file, wbook);
            WritableSheet sheet = book.getSheet(0);

            if (null != sheet.findCell(number)) {
                number = "";
            }

            ArrayList<String> list = new ArrayList<>();
            list.add(number.replace(" ", ""));
            list.add(operatingSteps.replace(" ", ""));
            list.add(expected.replace(" ", ""));
            list.add("pass");
            list.add(DateUtil.format(DateUtil.date(),"HH:mm:ss"));
            list.add(remark.replace(" ", ""));

            int columnum = sheet.getColumns();// 得到列数
            int rownum = sheet.getRows();// 得到行数

            DataAssert.jsonAssertions.put(rownum + "", executable);//将要执行的断言加入数组，在case最后统一执行

            WritableFont font = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false,
                    UnderlineStyle.NO_UNDERLINE, Colour.RED);
            WritableCellFormat fontFormat = new WritableCellFormat(font);
            for (int j = 0; j < columnum; j++) {

                if (list.get(j).equals("测试过程操作失败或出现异常")) {
                    Label labe = new Label(j, rownum, list.get(j), fontFormat);
                    sheet.addCell(labe);
                } else {
                    Label labe$ = new Label(j, rownum, list.get(j));
                    sheet.addCell(labe$);
                }
            }
            book.write();
            book.close();
        } catch (IOException | BiffException | WriteException e) {
            e.printStackTrace();
        }
    }

    public void printTestResult(String operatingSteps, String expected, Executable executable) {
        printTestResult(Config.TESTNUMBER, operatingSteps, expected, executable, "");
    }

    /**
     * 写入
     *
     * @param colum  excel 写入文件的列数
     * @param row    写入的行数
     * @param string 写入文件的信息
     * @param boo    true 写入的文本为红色，false 默认颜色
     */
    public void writeInFile(int colum, int row, String string, boolean boo) {
        try {
            File file = creatResultFile();
            Workbook wbook = Workbook.getWorkbook(file);
            WritableWorkbook book = Workbook.createWorkbook(file, wbook);
            WritableSheet sheet = book.getSheet(0);

            WritableFont font = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false,
                    UnderlineStyle.NO_UNDERLINE, Colour.RED);
            WritableCellFormat fontFormat = new WritableCellFormat(font);
            if (boo) {
                Label labe$ = new Label(colum, row, string, fontFormat);
                sheet.addCell(labe$);
            } else {
                Label labe$ = new Label(colum, row, string);
                sheet.addCell(labe$);
            }
            book.write();
            book.close();
        } catch (IOException | BiffException | WriteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 测试结果统计 输入测试结束时间、测试用例总数和测试失败率
     *
     * @throws Exception
     */
    public void statisticsResult() throws Exception {
        File file = creatResultFile();

        Workbook wbook = Workbook.getWorkbook(file);
        WritableWorkbook book = Workbook.createWorkbook(file, wbook);
        WritableSheet sheet = book.getSheet(0);
        // 设置字体样式
        WritableFont font = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, true,
                UnderlineStyle.NO_UNDERLINE, Colour.RED);
        WritableCellFormat fontFormat = new WritableCellFormat(font);
        WritableFont font$ = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, true,
                UnderlineStyle.NO_UNDERLINE, Colour.GREEN);
        WritableCellFormat fontFormat$ = new WritableCellFormat(font$);
        WritableFont font$$ = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD, false,
                UnderlineStyle.NO_UNDERLINE, Colour.DARK_BLUE2);
        WritableCellFormat fontFormat$$ = new WritableCellFormat(font$$);
        // 更新测试结束时间
        int rows = sheet.getRows();
        Label lable1 = new Label(1, 2, DateUtil.format(DateUtil.date(),"yyyy/MM/dd HH:mm:ss"), fontFormat$$);// 测试结束时间
        // 统计失败的条数
        int failCounts = 0;
        for (int i = 6; i < rows; i++) {
            Cell cell = sheet.getCell(3, i);
            if (cell.getContents().equals("fail")) {
                Label lable = new Label(3, i, "fail", fontFormat);
                sheet.addCell(lable);
                failCounts++;
            }
        }
        // 统计错误的条数
        int errorCounts = 0;
        for (int i = 6; i < rows; i++) {
            Cell cell = sheet.getCell(3, i);
            if (cell.getContents().equals("error")) {
                Label lable = new Label(3, i, "error", fontFormat);
                sheet.addCell(lable);
                errorCounts++;
            }
        }
        // 统计成功的条数
        int successCounts = 0;
        for (int i = 6; i < rows; i++) {
            Cell cell = sheet.getCell(3, i);
            if (cell.getContents().equals("pass")) {
                Label lable = new Label(3, i, "pass", fontFormat$);
                sheet.addCell(lable);
                successCounts++;
            }
        }
        System.out.println("fail总数：" + failCounts);
        System.out.println("error总数：" + errorCounts);
        System.out.println("pass总数：" + successCounts);
        // 测试失败率
        double scanle = Math.round((failCounts + errorCounts) * 10000 / (failCounts + successCounts + errorCounts)) / 100.0;
        System.out.println("失败率=" + scanle + "%");
        Label lable3 = new Label(1, 4, scanle + "%", fontFormat);
        // 测试用例总数
        Label lable2 = new Label(1, 3, (failCounts + successCounts + errorCounts) + "", fontFormat$$);

        sheet.addCell(lable1);
        sheet.addCell(lable2);
        sheet.addCell(lable3);
        book.write();
        book.close();
    }

}